package com.boat.framework.base.api;

import java.io.Serializable;

import com.boat.framework.base.exception.CommonErrorCode;
import com.boat.framework.base.exception.ErrorCode;

/**
 * 响应信息主体
 * 
 * @author 周江
 *
 * @param <T>
 */
public class Response<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	private String code;

	private String message;

	private T data;

	public Response() {
		super();
	}

	public String getCode() {
		return code;
	}

	public String getMessage() {
		return message;
	}

	public T getData() {
		return data;
	}

	public Response<T> setCode(String code) {
		this.code = code;
		return this;
	}

	public Response<T> setMessage(String message) {
		this.message = message;
		return this;
	}

	public Response<T> setData(T data) {
		this.data = data;
		return this;
	}
	
	public boolean isSuccess() {
		if(CommonErrorCode.SUCCESS.getCode().equals(code)) {
			return true;
		}
		
		return false;
	}
	// ------------------------领域方法--------------------------

	public static <T> Response<T> ok() {
		return ok(null);
	}

	public static <T> Response<T> ok(T data) {
		return new Response<T>().setCode(CommonErrorCode.SUCCESS.getCode()).setData(data)
				.setMessage(CommonErrorCode.SUCCESS.getMessage());
	}

	public static <T> Response<T> failed(String code, String message) {
		return new Response<T>().setCode(code).setMessage(message);
	}

	public static <T> Response<T> failed(ErrorCode failMsg) {
		return new Response<T>().setCode(failMsg.getCode()).setMessage(failMsg.getMessage());
	}

}
